package com.chzu.except;

import com.chzu.result.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州大健康与养老产业研究院
 * @ClassName GlobleException
 * @Description TODO
 * @Date 2022/01/27 21:44
 */
@Slf4j
@RestControllerAdvice
public class GlobleException {

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(value = RuntimeException.class)
    public JsonResult<Object> exception(RuntimeException e) {
        e.printStackTrace();
        log.error("系统运行时异常---->{}", e.getMessage());
        return JsonResult.fail(e.getMessage());
    }


    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ExceptionHandler(value = AccessDeniedException.class)
    public JsonResult<String> exception(AccessDeniedException e) {
        log.error("权限不足------> {}", e.getMessage());
        return JsonResult.fail("权限不足，请联系管理员");
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = UsernameNotFoundException.class)
    public JsonResult<Object> exception(UsernameNotFoundException e) {
        log.error("用户名不存在--->{}", e.getMessage());
        return JsonResult.fail("用户名不存在");
    }
}
